﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Konyvkolcsonzo
{
    public partial class FmPeldanyok : FormKK
    {
        List<peldany> nevsor;
        Peldanyok k;
       
        public FmPeldanyok()
        {
            InitializeComponent();
            nevsor = new List<peldany>();
            k = new Peldanyok();
            this.pnlLeft.Controls.Add(cmbKonyvek);
            this.pnlLeft.Controls.Add(lvwPeldany);
            this.pnlLeft.Controls.Add(btnAdd);
            this.pnlLeft.Controls.Add(btnEdit);
            this.pnlLeft.Controls.Add(btnDelete);
            this.pnlLeft.Controls.Add(btnClose);
            this.pnlLeft.Controls.Add(label1);
            this.pnlRight.Controls.Add(nudAr);
            this.pnlRight.Controls.Add(nudOldal);
            this.pnlRight.Controls.Add(cmbKiado);
            this.pnlRight.Controls.Add(nudEv);
            this.pnlRight.Controls.Add(btnOk);
            this.pnlRight.Controls.Add(btnCancel);
            this.pnlRight.Controls.Add(label3);
            this.pnlRight.Controls.Add(label4);
            this.pnlRight.Controls.Add(label5);
            this.pnlRight.Controls.Add(label6);
        }

        private void FmPeldanyok_Load(object sender, EventArgs e)
        {
            PopulateLvwPeldanyok();
            PopulateKonyvek();
            setNudEv();
            PopulateCmbKiadok();
        }

        private void setNudEv()
        {
            nudEv.Minimum = 1800;
            nudEv.Maximum = DateTime.Now.Year;
        }

        private void PopulateCmbKiadok()
        {
            cmbKiado.DataSource = k.getKiadok();
        }

        private void PopulateLvwPeldanyok() {
            lvwPeldany.Items.Clear();
            nevsor = k.getPeldany(cmbKonyvek.Text);
            for (int i = 0; i < nevsor.Count; i++)
            {
                ListViewItem sor = new ListViewItem(Convert.ToString(nevsor[i].id));
                sor.SubItems.Add(Convert.ToString(nevsor[i].cim));
                sor.SubItems.Add(Convert.ToString(nevsor[i].kiadoID));
                sor.SubItems.Add(Convert.ToString(nevsor[i].kiado));
                sor.SubItems.Add(Convert.ToString(nevsor[i].ev));
                sor.SubItems.Add(Convert.ToString(nevsor[i].oldal));
                sor.SubItems.Add(Convert.ToString(nevsor[i].ar));
                lvwPeldany.Items.Add(sor);
            }

            if (lvwPeldany.SelectedItems.Count == 0 && lvwPeldany.Items.Count > 0)
            {
                lvwPeldany.Items[0].Selected = true;
            }
        }

        private void PopulateKonyvek()
        {
            cmbKonyvek.DataSource = k.getKonyvek();
        }

        private void PopulatePanelRight(long id)
        {
            nudAr.Value = Convert.ToInt32(lvwPeldany.SelectedItems[0].SubItems[6].Text);
            nudOldal.Value = Convert.ToInt32(lvwPeldany.SelectedItems[0].SubItems[5].Text);
            nudEv.Value = Convert.ToInt32(lvwPeldany.SelectedItems[0].SubItems[4].Text);
        }

        private void PopulatePanelRight()
        {
            nudAr.Value = nudAr.Minimum;
            nudOldal.Value = nudOldal.Minimum;
            nudEv.Value = nudEv.Maximum;
        }

        private void cmbKonyvek_SelectedIndexChanged(object sender, EventArgs e)
        {
            PopulateLvwPeldanyok();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.cmdAdd();
            PopulatePanelRight();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (this.cmdEdit(lvwPeldany.SelectedItems.Count))
            {
                PopulatePanelRight(getPeldanyID(lvwPeldany.SelectedItems[0].SubItems[0].Text));
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.cmdDelete(lvwPeldany.SelectedItems.Count))
            {
                k.deletePeldany(getPeldanyID(lvwPeldany.SelectedItems[0].SubItems[0].Text));
                PopulateLvwPeldanyok();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.cmdClose();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.cmdExit();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.cmdOK();
            peldany uj = new peldany();
            uj.ar = Convert.ToInt32(nudAr.Value);
            uj.oldal = Convert.ToInt32(nudOldal.Value);
            uj.kiado = Convert.ToString(cmbKiado.SelectedItem);
            uj.cim = Convert.ToString(cmbKonyvek.Text);
            uj.ev = Convert.ToInt32(nudEv.Value);

            if (this.editType())
            {
                uj.id = Convert.ToString(lvwPeldany.SelectedItems[0].SubItems[0]).Substring(24,4);
                k.updatePeldany(uj);
            }
            else
            {
                k.insertPeldany(uj);
            }
            PopulateLvwPeldanyok();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.cmdCancel();
        }

        private long getPeldanyID(string kod){
            long id = Convert.ToInt64(kod.Substring(6));
            return id;
        }

    }
}
